// 规格管理页面
<template>
  <div class="app-container">
    <div class="cbox">
      <el-form
        :model="compForm"
        :inline="true"
        ref="compForm"
        size="mini"
        :rules="compFormRules"
      >
        <!-- <el-row> -->
          <!-- <el-col :span="4"> -->
            <el-form-item label="规格名称:" prop="normName">
              <el-input  clearable maxlength="16" style="width:180px" v-model="compForm.normName"></el-input>
            </el-form-item>
          <!-- </el-col> -->
          <!-- <el-col :span="4"> -->
            <el-form-item label="规格代码:" prop="normCode">
              <el-input  clearable maxlength="16" style="width:180px" v-model="compForm.normCode"></el-input>
            </el-form-item>
          <!-- </el-col> -->
          <!-- <el-col :span="2"> -->
            <el-form-item>
              <el-button type="primary" @click="search">查询</el-button>
            </el-form-item>
          <!-- </el-col> -->
        <!-- </el-row> -->
      </el-form>
    </div>
    <el-row :gutter="10" class="btn-area">
      <el-col :span="24">
        <buttonName @clickEvent="clickEventDom"></buttonName>
        <!-- <el-button size="mini" type="primary" v-if="isAuth('add')" plain @click="add"
          >新增规格</el-button
        >
        <el-button size="mini" type="primary" v-if="isAuth('edit')" plain @click="edit"
          >编辑规格</el-button
        >
        <el-button size="mini" type="primary" v-if="isAuth('del')" plain @click="del"
          >删除规格</el-button
        > -->
      </el-col>
    </el-row>
    <div class="table">
      <div class="inner" ref="inner">
        <el-table
          :data="compList"
          class="charge"
          :highlight-current-row="true"
          @row-click="getCurrentRow"
          @row-dblclick="storageDetail()"
          border
          :header-cell-style="{
            textAlign: 'center'
          }"
          :height="maxHeight"
        >
          <el-table-column size="mini"  align="center" label="序号" type="index"  :index="indexMethod" show-overflow-tooltip  width="60" ></el-table-column>
          <el-table-column size="mini"  align="left" label="规格名称"  prop="normName" show-overflow-tooltip></el-table-column>
          <el-table-column size="mini" align="left" label="规格代码"  prop="normCode" show-overflow-tooltip ></el-table-column>
          <el-table-column size="mini" align="left" label="创建员工" prop="creator" show-overflow-tooltip ></el-table-column>
          <el-table-column size="mini" align="center"  label="创建时间"  prop="created" show-overflow-tooltip ></el-table-column>
          <el-table-column size="mini" align="left"  label="最后修改员工" prop="lastModifier" show-overflow-tooltip ></el-table-column>
          <el-table-column size="mini" align="center" label="最后修改时间" prop="lastModified" show-overflow-tooltip ></el-table-column>
          <el-table-column size="mini" align="center" label="备注" prop="remark" show-overflow-tooltip ></el-table-column>
          <el-table-column
            align="left"
            label="是否有效"
            prop="isvalid"
            show-overflow-tooltip
          >
             <template slot-scope="scope">
                   <div style="text-align: center;" v-if="scope.row.isvalid == 'Y'">
                        <el-checkbox v-model="item.item" disabled></el-checkbox>
                   </div>
                    <div style="text-align: center;" v-if="scope.row.isvalid == 'N'">
                       <el-checkbox v-model="item.itemA" disabled></el-checkbox>
                 </div>
              </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="是否默认"
            prop="isdefault"
            show-overflow-tooltip
          >
             <template slot-scope="scope">
                   <div style="text-align: center;" v-if="scope.row.isdefault == 'Y'">
                        <el-checkbox v-model="item.item" disabled></el-checkbox>
                   </div>
                    <div style="text-align: center;" v-if="scope.row.isdefault == 'N'">
                       <el-checkbox v-model="item.itemA" disabled></el-checkbox>
                 </div>
              </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <el-pagination
      class="pagination"
      small
      background
      ref="pagination"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="Pagination.currentPage"
      :page-sizes="Pagination.pageSizeList"
      :page-size="Pagination.pageSize"
      :layout="Pagination.layout"
      :total="Pagination.total"
    ></el-pagination>

    <el-dialog
      width="700px"
      v-el-drag-dialog
      ref="dialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      v-if="showDialog"
      :title="operate"
      :before-close="handleClose">
      <el-form
        :model="dialogForm"
        ref="dialogForm"
        label-width="10rem"
        size="mini"
        :rules="dialogFormRules"
      >
       
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格名称：" prop="normName" >
              <el-input minlength="1"  maxlength="10" clearable :disabled="messageDisabled" v-model="dialogForm.normName" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格代码："  prop="normCode" >
              <el-input minlength="1" maxlength="10"  clearable :disabled="see" @change="uppercase('normCode')" v-model="dialogForm.normCode"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="是否有效："
              :disabled="messageDisabled"
              :prop="messageDisabled == false ? 'isvalid' : ''"
            >
              <el-radio-group
                :disabled="messageDisabled"
                v-model="dialogForm.isvalid"
              >
                <el-radio label="Y">是</el-radio>
                <el-radio label="N">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item
              label="是否默认："
              :disabled="messageDisabled"
              :prop="messageDisabled == false ? 'isdefault' : ''"
            >
              <el-radio-group
                :disabled="messageDisabled"
                v-model="dialogForm.isdefault"
              >
                <el-radio label="Y">是</el-radio>
                <el-radio label="N">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item prop="remark" label="备注：">
              <el-input maxlength="32" type="textarea" :disabled="messageDisabled" v-model="dialogForm.remark" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="close('dialogForm')">关闭</el-button>
        <el-button  size="mini"  v-if="messageDisabled == false"  @click="save"  type="primary" >保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getNormList,addNorm,editNorm,delNorm,getCode,getPCList, addPC, editPC, delPC ,provinceCityGj} from '@/request/api'
import resizeMixin from '@/mixins/resize'
import isAuth from '@/mixins/buttonAuth'
import paginationMixin from '@/mixins/pagination'
import scrollChange from '@/utils/onscroll.js'
import buttonName from '@/components/buttonName/button-name.vue'

export default {
  name: 'router.provinceCity',
  mixins: [resizeMixin, paginationMixin,isAuth],
  data() {
    return {
      dialogFormStatus: '', //dialog 中 form 的状态 add/edit
      accountDisabled: true,
      messageDisabled: false,
      see: true,
      currentData: {},
      showDialog: false,
      isChange: '',
      maxHeight: -1,
      operate: '',
      countryList: '',
      compForm: {
        normName: '',
        normCode:''
      },
      compFormRules: {
        cName: [
          {
            pattern: /^[\u4e00-\u9fa5]+$/,
            message: '请输入中文汉字',
            trigger: 'change'
          }
        ]
      },
      item:{
        item:true,
        itemA:false
      },
      options: [
        {
          value: 'Y',
          label: '激活'
        },
        {
          value: 'N',
          label: '未激活'
        }
      ],
      dialogForm: {
        isvalid:'Y',
        isdefault:'Y'
      },
      dialogFormRules: {
        prvShrchn:[
          {
            pattern: /^[\u4e00-\u9fa5]+$/,
            message: '请输入中文',
            trigger: 'blur'
          }
        ],
        normName:[
          {
            required: true,
            message: '规格名称不能为空',
            trigger: 'blur'
          }
        ],
        normCode:[
          {
            required: true,
            message: '规格代码不能为空',
            trigger: 'blur'
          }
        ],

        prvCtycd: [
          {
            required: true,
            message: '国家名称不能为空',
            trigger: 'change'
          }
        ],
        isvalid: [
          {
            required: true,
            message:'有效标志不能为空',
            trigger: 'blur'
          }
        ],
        isdefault: [
          {
            required: true,
            message:'是否默认不能为空',
            trigger: 'blur'
          }
        ],
        prvChnname: [
          {
            required: true,
            message: '中文名称不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[\u4e00-\u9fa5]+$/,
            message: '请输入中文',
            trigger: 'blur'
          }
        ],
        prvEgsname: [
          {
            required: true,
            message: '英文名称不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z ]+$/,
            message: '请输入英文',
            trigger: 'blur'
          }
        ],
        prvCode: [
          {
            required: true,
            message: '省市代码不能为空',
            trigger: 'blur'
          },
          {
            pattern: /^[a-zA-Z0-9]+$/,
            message: '请输入英文和数字',
            trigger: 'blur'
          }
        ]
      },
      compList: [],
      GJList:[],
      GJListA:[]
    }
  },
  // computed: {
  //   grandson() {
  //     var dataList = []
  //     var number
  //     var arr = this.$route.path.split('/')
  //     number = arr.length - 1
  //     dataList = this.$store.state.user.permList
  //     for (var i = 0; i < dataList.length; i++) {
  //       for (var f = 0; f < dataList[i].Children.length; f++) {
  //         if (dataList[i].Children[f].MDL_CODE == arr[number]) {
  //           return dataList[i].Children[f].Grandson
  //         }
  //       }
  //     }
  //     return []
  //   }
  // },
  components: {
    buttonName
  },
  mounted() {
    this.refreshList()
    let demandWrapper2=document.querySelector(".charge .el-table__body-wrapper");
    // 新增滚动条监听事件
    scrollChange(demandWrapper2);
  },
  created() {
    this.resize = () => {
      this.maxHeight = this.$refs.inner.offsetHeight
    }
  },
  methods: {
    //国家下拉框
    GjData(){
      this.GJList = []
      provinceCityGj({
        ctyCd:'',
        ctyCname:'',
        ctyEname:'',
      }).then(res =>{
        this.GJListA = res.data
        let box = []
        for(let  i=0; i< this.GJListA.length; i++ ){
          if(this.GJListA[i].ctyIsvalid == 'Y'){
            box.push(this.GJListA[i])
            // this.GJList.push(this.GJListA)
            // console.log(this.GJList)
          }
        }
        this.GJList=box

      })
    },
    indexMethod (index) {
      return 1 + index + (Number(this.Pagination.currentPage) - 1) * Number(this.Pagination.pageSize)
    },
    // checkCode() {
    //   this.compForm.cName = this.compForm.cName.replace(/[^\u4e00-\u9fa5]/g, '')
    // },
    uppercase(key) {
      this.dialogForm[key] = this.dialogForm[key].toUpperCase()
    },
    uppercaseA(key) {
      this.dialogForm[key] = this.dialogForm[key].toUpperCase()
    },
    trimECode() {
      this.dialogForm.PRV_EGSNAME = this.dialogForm.PRV_EGSNAME.trim()
    },
    // 点击查询
    search() {
      this.refreshList()
    },
    //获取列表数据
    getList(currentPage, pageSize, updateTotal) {
      getNormList({
        normName: this.compForm.normName,
        normCode:this.compForm.normCode,
        PageIndexs:this.Pagination.currentPage.toString(),
        PageSizes:this.Pagination.pageSize.toString()
      }).then(response => {
        this.compList = response.data
        updateTotal(Number(response.total))
      })
    },
    // 新增
    add() {
      this.showDialog = true
      this.isChange = ''
      this.operate = '新增规格'
      this.accountDisabled = false
      this.messageDisabled = false
      
      this.dialogFormStatus = 'add'
      if (this.$refs['dialogForm']) {
        this.$refs['dialogForm'].resetFields()
      }
      this.resetDialogForm()
      getCode().then(res => {
        this.dialogForm.normCode = res.msg
      })
    },
    // 编辑
    edit() {
      this.dialogFormStatus = 'edit'
      this.isChange = false
      if (Object.keys(this.currentData).length == 0) {
        this.$alert('请选择需要编辑的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }
      this.showDialog = true
      this.operate = '编辑规格'
      this.accountDisabled = true
      this.messageDisabled = false
      this.dialogForm = Object.assign({}, this.currentData)
      if (this.$refs['dialogForm']) {
        this.$refs['dialogForm'].resetFields()
      }

    },
    // 删除
    del() {
      if (Object.keys(this.currentData).length == 0) {
        this.$alert('请选择需要删除的数据', '提示', {
          confirmButtonText: '确定'
        })
        return
      }

      this.$confirm('您确认要删除此数据吗?', '提示', {
        closeOnClickModal: false
      })
        .then(() => {
          delNorm({id:this.currentData.id}).then(() => {
            this.$message.success({
              message: '删除成功！',
              showClose: true
            })
            this.refreshList()
          })
        }).catch(() => {})
    },
    //点击保存
    save() {
      // this.trimECode()
      if (this.dialogFormStatus === 'add') {
        this.commitAdd('dialogForm')
      } else if (this.dialogFormStatus === 'edit') {
        this.commitEdit('dialogForm')
      }
    },
    commitAdd(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          addNorm(this.dialogForm).then(() => {
            this.$message.success({
              message: '新增数据成功！',
              showClose: true
            })
            this.showDialog = false
            this.refreshList()
          })
        }
      })
    },
    //点击编辑
    commitEdit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          editNorm(this.dialogForm).then(() => {
            this.$message.success({
              message: '编辑数据成功！',
              showClose: true
            })
            this.showDialog = false
            this.dialogForm = {}
            this.currentData = {}
            this.refreshList()
          })
        }
      })
    },
    //关闭弹出框
    close(formName) {
      this.$refs[formName].resetFields()
      this.dialogForm = {}
      this.currentData = {}
      this.showDialog = false
      this.refreshList()
    },
    //新增初始数据
    resetDialogForm() {
      this.dialogForm = {
        normCode: '', 
        normName: '', 
        // ctyCname: '',
        isdefault:'N',
        isvalid: 'Y', 
        remark: '' 
      }
    },
    // 获取当前选中行
    getCurrentRow(row) {
      this.currentData = row
    },
    //双击查看
    storageDetail() {
      this.showDialog = true
      this.isChange = ''
      this.operate = '查看省信息'
      this.accountDisabled = true
      this.messageDisabled = true
      this.dialogForm = Object.assign({}, this.currentData)
    },
    //调取父组件方法
    clickEventDom(item){
      if(item.mdlCode === 'add'){
        this.add()
      }else if(item.mdlCode === 'edit'){
        this.edit()
      }else if(item.mdlCode === 'del'){
        this.del()
      }
    }
    // handleClose(done) {
    //   if (this.$refs.dialogForm) {
    //     this.$refs.dialogForm.resetFields()
    //     done()
    //   }
    // },
    // getChanges() {
    //   var lists = []
    //   for (let i of this.countryList) {
    //     lists.push(i.UUID)
    //   }
    //   if (lists.indexOf(this.dialogForm.PRV_CTY_UUID) !== -1) {
    //     return
    //   } else {
    //     this.isChange = this.dialogForm.PRV_CTY_UUID
    //     console.log('s')
    //     this.$set(
    //       this.dialogForm,
    //       'PRV_CTY_UUID',
    //       this.dialogForm.PRV_CTY_CNAME
    //     )
    //   }
    // }
  }
}
</script>

<style lang="scss" scoped>

.el-select {
  width: 100%;
}

.cbox{
  margin-left: -8px;
}

.app-container {
  height: 100%;
  width: 100%;
  position: relative;
  display: flex;
  flex-direction: column;

  .table {
    flex: 1;
    position: relative;
    overflow: hidden;

    .inner {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }

    .el-pagination {
      height: 4rem;
      flex: 0 0 auto;
      margin: 1rem 0;
    }
  }
}
</style>
